package com.chacanks.common.util;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * <pre>
 * 날짜관련 유틸리티
 * 
 *  - 의존 라이브러리 : commons-lang-2.6.jar(org.apache.commons.lang.time.DateUtils)
 * </pre>
 * @author : Lee Kang-Sun
 * @Date : 2013. 1. 8.
 */
public class DateUtils extends org.apache.commons.lang.time.DateUtils{
	
	public static Locale DEFAULT_LOCALE = Locale.KOREA;	

	public static final String NONE_DATE_FORMAT			= "yyyyMMdd";
	public static final String DASH_DATE_FORMAT			= "yyyy-MM-dd";
	public static final String SLASH_DATE_FORMAT		= "yyyy/MM/dd";

	public static final String DEFAULT_DATE_FORMAT		= NONE_DATE_FORMAT;

	public static final String[] formatList = {"-", "/", "_"};

	/**
	 * <pre>
	 * 현재 날짜
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @return
	 */
	public static String getFullDate(){
		return getFullDate( new Date(), DEFAULT_DATE_FORMAT );
	}

	/**
	 * <pre>
	 * 현재 날짜를 해당 포멧형식으로 변경 
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @param format
	 * @return
	 */
	public static String getFullDate(String format){
		return getFullDate( new Date(), format );
	}

	/**
	 * <pre>
	 * 특정 날짜
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @param date
	 * @return
	 */
	public static String getFullDate(Date date){
		return getFullDate( date, DEFAULT_DATE_FORMAT );
	}

	/**
	 * <pre>
	 * 특정 날짜를 해당 포멧형식으로 변경
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @param date
	 * @param format
	 * @return
	 */
	public static String getFullDate(Date date, String format){
		return getSimpleDateFormat( format ).format( date );
	}

	/**
	 * <pre>
	 * 현재 날짜에 년 변경
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @param amount
	 * @return
	 */
	public static String setYearsToString(int amount){
		return setYearsToString(new Date(), amount);
	}
	
	/**
	 * <pre>
	 * 현재 날짜에 년 변경
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @param amount
	 * @param format
	 * @return
	 */
	public static String setYearsToString(int amount, String format){
		return setYearsToString(new Date(), amount, format);
	}
	
	/**
	 * <pre>
	 * 기준 날짜에 년 변경
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @param date
	 * @param amount
	 * @return
	 */
	public static String setYearsToString(Date date, int amount){
		return getSimpleDateFormat().format( setYears(date, amount) );
	}
	
	/**
	 * <pre>
	 * 기준 날짜에 년 변경
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @param date
	 * @param amount
	 * @param format
	 * @return
	 */
	public static String setYearsToString(Date date, int amount, String format){
		return getSimpleDateFormat(format).format( setYears(date, amount) );
	}
	
	/**
	 * <pre>
	 * 현재 날짜에 월 변경
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @param amount
	 * @return
	 */
	public static String setMonthsToString(int amount){
		return setMonthsToString(new Date(), amount);
	}
	
	/**
	 * <pre>
	 * 현재 날짜에 월 변경
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @param amount
	 * @param format
	 * @return
	 */
	public static String setMonthsToString(int amount, String format){
		return setMonthsToString(new Date(), amount, format);
	}
	
	/**
	 * <pre>
	 * 기준날짜에 월 변경
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @param date
	 * @param amount
	 * @return
	 */
	public static String setMonthsToString(Date date, int amount){
		return getSimpleDateFormat().format( setMonths(date, amount) );
	}
	
	/**
	 * <pre>
	 * 기준날짜에 월 변경
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @param date
	 * @param amount
	 * @param format
	 * @return
	 */
	public static String setMonthsToString(Date date, int amount, String format){
		return getSimpleDateFormat(format).format( setMonths(date, amount) );
	}
	
	/**
	 * <pre>
	 * 현재날짜에 일 변경
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @param amount
	 * @return
	 */
	public static String setDaysToString(int amount){
		return setDaysToString(new Date(), amount);
	}
	
	/**
	 * <pre>
	 * 현재날짜에 일 변경
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @param amount
	 * @param format
	 * @return
	 */
	public static String setDaysToString(int amount, String format){
		return setDaysToString(new Date(), amount, format);
	}
	
	/**
	 * <pre>
	 * 기준날짜에 일 변경
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @param date
	 * @param amount
	 * @return
	 */
	public static String setDaysToString(Date date, int amount){
		return getSimpleDateFormat().format( setDays(date, amount) );
	}
	
	/**
	 * <pre>
	 * 기준날짜에 일 변경
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @param date
	 * @param amount
	 * @param format
	 * @return
	 */
	public static String setDaysToString(Date date, int amount, String format){
		return getSimpleDateFormat(format).format( setDays(date, amount) );
	}

	/**
	 * <pre>
	 * 문자열로 구성된 날짜를 기본 포멧형식으로 지정 후 Date 리턴
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @param fullDate
	 * @return
	 */
	public static Date stringToDate(String fullDate){
		return stringToDate(fullDate, DEFAULT_DATE_FORMAT);
	}

	/**
	 * <pre>
	 * 문자열로 구성된 날짜를 입력된 포멧형식으로 지정 후 Date 리턴
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @param fullDate
	 * @param format
	 * @return
	 */
	public static Date stringToDate(String fullDate, String format){
		return getSimpleDateFormat(format).parse(fullDate, new ParsePosition(0));
	}

	/**
	 * <pre>
	 * 문자열로된 날짜의 포멧형식을 제거.
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @param dateStr
	 * @return
	 */
	public static String removeFormat(String dateStr){

		try{
			for(String format : formatList){
				
				if(dateStr.length() == 8) return dateStr;
				
				if(dateStr.indexOf(format) > -1){
					dateStr = dateStr.replaceAll(format, "");
					return dateStr;
				}
				
			}			
		}catch(Exception e){
			return dateStr;
		}

		return dateStr;
	}

	/**
	 * <pre>
	 * 현재 년도(YYYY)
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @return
	 */
	public static int getYear(){
		return getCalendar().get(Calendar.YEAR);
	}
	
	/**
	 * <pre>
	 * 현재 년도(YYYY)
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @return
	 */
	public static String getYearToString(){
		return Integer.toString( getYear() );
	}

	/**
	 * <pre>
	 * 현재월(MM)
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @return
	 */
	public static int getMonth(){
		return getCalendar().get(Calendar.MONTH)+1;
	}

	/**
	 * <pre>
	 * 현재월(MM)
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @return
	 */
	public static String getMonthToString(){
		return StringUtils.leftPad(Integer.toString(getMonth()), 2, "0");
	}
	
	/**
	 * <pre>
	 * 현재일(DD)
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @return
	 */
	public static int getDay(){
		return getCalendar().get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * <pre>
	 * 현재일(DD)
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @return
	 */
	public static String getDayToString(){
		return StringUtils.leftPad(Integer.toString( getDay() ), 2, "0");
	}

	/**
	 * <pre>
	 * 오늘 날짜의 Calendar 생성
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @return
	 */
	private static Calendar getCalendar(){
		return Calendar.getInstance();
	}

	/**
	 * <pre>
	 * Date -> Calendar
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @param date
	 * @return
	 */
	public static Calendar getTargetCalendar(Date date){
		Calendar calendar = getCalendar();
		calendar.setTime(date);
		return calendar;
	}

	/**
	 * <pre>
	 * SimpleDateFormat 생성 ( format : DEFAULT_DATE_FORMAT, locale : DEFAULT_LOCALE )
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @return
	 */
	private static SimpleDateFormat getSimpleDateFormat(){
		return getSimpleDateFormat(DEFAULT_DATE_FORMAT);
	}

	/**
	 * <pre>
	 * SimpleDateFormat 생성 ( format : 사용자지정, locale : DEFAULT_LOCALE )
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @param format
	 * @return
	 */
	private static SimpleDateFormat getSimpleDateFormat(String format){
		return getSimpleDateFormat(format, DEFAULT_LOCALE);
	}

	/**
	 * <pre>
	 * SimpleDateFormat 생성 ( format, locale : 사용자 지정 )
	 * </pre>
	 * @author : Lee Kang-Sun
	 * @Date : 2013. 1. 8.
	 * @param format
	 * @param locale
	 * @return
	 */
	private static SimpleDateFormat getSimpleDateFormat(String format, Locale locale){
		return new SimpleDateFormat(format, locale);
	}
	
	/*
	======== [ commons-lang-2.6.jar(org.apache.commons.lang.time.DateUtils) ] ========
		public static Date setDays(Date date, int amount) {
			return set(date, Calendar.DAY_OF_MONTH, amount);
		}

		public static Date setYears(Date date, int amount) {
			return set(date, Calendar.YEAR, amount);
		}

		public static Date setMonths(Date date, int amount) {
			return set(date, Calendar.MONTH, amount);
		}

		private static Date set(Date date, int calendarField, int amount) {
			if (date == null) {
				throw new IllegalArgumentException("The date must not be null");
			}
			Calendar c = Calendar.getInstance();
			c.setLenient(false);
			c.setTime(date);
			c.set(calendarField, amount);
			return c.getTime();
		}
	*/
}
